<%--
  Created by IntelliJ IDEA.
  User: fxsun
  Date: 2021/4/15
  Time: 11:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>RSA</title>
    <style>
        div {
            margin-top: 10px;
        }
    </style>
</head>
<body style="margin: 30px;">
<div>
    <table>
        <tr>
            <td>
                公匙:<br>
                <textarea id="publicKey" style="width: 500px; height: 200px;">MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCYO23qMcvv0gpGs97rfmxITjjz5m/sjZ4JgYvPBs+JHwD/SCv7TJCEKVm4aXTEPTXRkN7LgUjQTnEGGS1JBY4zyqpnmJcfpNxWpdgAyE/4poMJ8rcZvqm14DOjNH+69mwpIdJkySHuzUCzQtMJ8GG2owNc9frTOpZozlcUWbGWfQIDAQAB</textarea>
            </td>
            <td>
                私匙:<br>
                <textarea id="privateKey" style="width: 500px; height: 200px;">MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJg7beoxy+/SCkaz3ut+bEhOOPPmb+yNngmBi88Gz4kfAP9IK/tMkIQpWbhpdMQ9NdGQ3suBSNBOcQYZLUkFjjPKqmeYlx+k3Fal2ADIT/imgwnytxm+qbXgM6M0f7r2bCkh0mTJIe7NQLNC0wnwYbajA1z1+tM6lmjOVxRZsZZ9AgMBAAECgYAoGzAgDTJ/YkTWz7ihLlN00Tbr+v/twHmsY3bj+hVfOM3Yc7kyob9JMmOy1AWxVbcCGTq5PrxiNOhOBQALRu1pivDsGDm2wA+Jb4vSIwL8dFCnIMlFFPMUb5VwtNIdlEZDQdZoLV7185OJ4IEK//GuNTwCklZNIOZ0j0YMEUw7uQJBAN5C3QpRWfwRhDf1S5+RaQPZWKehuc3DU2T67/tIUOKkSnY/KUX4KxPxCXXRsrbzU4mwoW0npBO3mtuOFDsk788CQQCvVzdjz7TZXFcGOjhk1e+id/ElW69/nt6DQmwUnJYyNS4cYfKoGD/RAxN0xaAoXsl6u3FGzY7TjLsuf6Wn3vvzAkAmLGW6d+50lK2YztCGP3tB5fqMEALRjFKubUr6ZZk+0+jWFlMIaW88pZFyYunG8lPOuj9/d+d+W3KFcwmWfumRAkApho1OrSVWiQDvL6CleOk84A0TXOhuYBCwo213YDJOB7w46pWOa9fJR2I3OIqapQAwee3057/YqC64b3CrFiszAkEAttDMWhXlagXMjwZAjEuuHK3CVCqRPNhZlnoX5A5RWIg0+BpHNQDfVSlYBtki361zB8L3HR8YfJIOWk/4S+ORLg==</textarea>
            </td>
        </tr>
    </table>
</div>
<div>
    <input id='pwd' placeholder="请输入密码"/>
    <button onclick="encryptPwd()">加密</button>
</div>
<div>
    <textarea id="result" style="width: 500px; height: 100px;"></textarea>
</div>
<div>
    <button onclick="decryptPwd()">解密</button>
</div>
<div>
    <textarea id="pPwd" style="width: 500px; height: 100px;">加解密后的密码</textarea>
</div>
</body>

<script src="script/jsencrypt.js" type="text/javascript"></script>

<script>
    function $q(id) {
        return document.querySelector('#' + id);
    }

    function encryptPwd() {
        let pwd = $q('pwd').value;
        let encrypt = new JSEncrypt();
        let publicKey = document.getElementById("publicKey").value;
        encrypt.setPublicKey(publicKey);
        let res1 = encrypt.encrypt(pwd);
        //var ps= encodeURI(d).replace(/\+/g, '%2B');
        $q('result').value = res1;
    }

    function decryptPwd() {
        let result = $q('result').value;
        let jsEncrypt = new JSEncrypt();
        let privateKey = document.getElementById("privateKey").value;
        jsEncrypt.setPrivateKey(privateKey);
        let res = jsEncrypt.decrypt(result);
        $q('pPwd').value = res;
    }
</script>
</html>
